package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.thinkcube.edutainment.R;



@EFragment(R.layout.audio_fragment)
public class AudioFragment extends Fragment implements OnClickListener,OnPreparedListener,OnCompletionListener,OnErrorListener
{
	
	

	MediaPlayer mp;
	ProgressDialog pd;
	
	@ViewById(R.id.play_audio)
	Button play;

	@Override
	public void onCompletion(MediaPlayer arg0) {
		 pd.dismiss();
	        Toast.makeText(getActivity(), "Completed", Toast.LENGTH_LONG).show();
		
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		  Log.i("StreamAudioDemo", "prepare finished");
	       pd.setMessage("Playing.....");
	       mp.start();
		
	}

	@Override
	public void onClick(View v) {
		
		
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		 pd.dismiss();
	     return false;
	}
	
	
	@Click(R.id.play_audio)
	void palyAudio()
	{
		 try
	        {
	            pd = new ProgressDialog(getActivity());
	            pd.setMessage("Buffering.....");
	            pd.show();
	            mp = new MediaPlayer();
	            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
	            mp.setOnPreparedListener(this);
	            mp.setOnErrorListener(this);
	            mp.setDataSource("http://www.robtowns.com/music/blind_willie.mp3");
	            mp.prepareAsync();
	           
	            mp.setOnCompletionListener(this);
	            
	        }
	        catch(Exception e)
	        {
	            Log.e("StreamAudioDemo", e.getMessage());
	        }
	}

}
